﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Reports_ServiceInvoice_InvoiceViews_BillsAndReceipts : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    AccountSettings acntstng = new AccountSettings();
    DataTable dtMain1 = new DataTable();
    DataTable dtMain2 = new DataTable();
    string finyear = "";

    Double totalTaxable = 0;
    Double totalNonTaxable = 0;
    Double totalReimbursementAmount = 0;
    Double totalServiceTax = 0;
    Double totalPECess = 0;
    Double totalHECess = 0;
    Double totalReceiptTaxable = 0;
    Double totalReceiptServiceTax = 0;
    Double totalReceiptPECess = 0;
    Double totalReceiptHECess = 0;

    Double totalTaxableN = 0;
    Double totalNonTaxableN = 0;
    Double totalReimbursementAmountN = 0;
    Double totalServiceTaxN = 0;
    Double totalPECessN = 0;
    Double totalHECessN = 0;
    Double totalReceiptTaxableN = 0;
    Double totalReceiptServiceTaxN = 0;
    Double totalReceiptPECessN = 0;
    Double totalReceiptHECessN = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            finyear = Session["finyear"].ToString();
            hidfinyear.Value = Session["finyear"].ToString();
            txtFromDate.Text = Convert.ToString("01/04/" + finyear);
            txtToDate.Text = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
            BindGrid(txtFromDate.Text, txtToDate.Text);
            lblCompName.Text = Session["compname"].ToString();
            lblCompName2.Text = Session["compname"].ToString();
        }
    }

    protected void BindGrid(string from, string to)
    {
        if (from != string.Empty && to != string.Empty)
        {
            SqlParameter[] sqlparam = {
                                      new SqlParameter("@Finyear",hidfinyear.Value.ToString()),
                                      new SqlParameter("@FROMDATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(from))),
                                      new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(to))),
                                  };
            dtMain1 = DU.getdatausingprocedure("USP_IVC_GetBillwiseDNReceipts", sqlparam);
            dtMain2 = DU.getdatausingprocedure("USP_IVC_GetBillwiseBillsReceipts", sqlparam);
        }
        lblMsg.Text = "Bills & Receipt Register : From " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(from)) + " To " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(to)) + ".";
        lblMsg2.Text = "Bills & Receipt Register : From " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(from)) + " To " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(to)) + ".";
        dtMain1.Merge(dtMain2);
        grdBillsReceiptView.DataSource = dtMain1;
        grdBillsReceiptView.DataBind();
        grdPrint.DataSource = dtMain1;
        grdPrint.DataBind();
    }

    protected void grdBillsReceiptView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Attributes.Add("rowspan", "2");
            e.Row.Cells[1].Attributes.Add("colspan", "6");
            e.Row.Cells.RemoveAt(6);
            e.Row.Cells.RemoveAt(5);
            e.Row.Cells.RemoveAt(4);
            e.Row.Cells.RemoveAt(3);
            e.Row.Cells.RemoveAt(2);
            e.Row.Cells[1].Text = "Bills";
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[2].Attributes.Add("colspan", "4");
            e.Row.Cells.RemoveAt(5);
            e.Row.Cells.RemoveAt(4);
            e.Row.Cells.RemoveAt(3);
            e.Row.Cells[2].Text = "Receipts";
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;
            //new row//

            string strNewRow = "</th></tr><tr><th class=\"gridheader\" style='width:150px'>Taxable</th><th class=\"gridheader\" style='width:150px'>NonTaxable</th><th class=\"gridheader\" style='width:150px'>Reimbursement</th><th class=\"gridheader\" style='width:150px'>Service Tax</th><th class=\"gridheader\" style='width:150px'>P.E.Cess</th><th class=\"gridheader\" style='width:150px'>H.E.Cess</th>";
            strNewRow += "<th class=\"gridheader\">Taxable</th><th class=\"gridheader\">ServiceTax</th> <th class=\"gridheader\">P.E.Cess</th><th class=\"gridheader\">H.E.Cess";
            e.Row.Cells[2].Text += strNewRow;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double TaxableAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TaxableAmount"));
            Double NonTaxableAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "NonTaxableAmount"));
            Double ReimbursementAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReimbursementAmount"));
            Double ServiceTax1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ServiceTax1"));
            Double PECess1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "PECess1"));
            Double HECess1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "HECess1"));
            Double ReceiptTaxable = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReceiptTaxable"));
            Double ReceiptServiceTax = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReceiptServiceTax"));
            Double ReceiptPECess = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReceiptPECess"));
            Double ReceiptHECess = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReceiptHECess"));

            totalTaxable = totalTaxable + Math.Abs(TaxableAmount);
            totalNonTaxable = totalNonTaxable + Math.Abs(NonTaxableAmount);
            totalReimbursementAmount = totalReimbursementAmount + Math.Abs(ReimbursementAmount);
            totalHECess = totalHECess + Math.Abs(HECess1);
            totalPECess = totalPECess + Math.Abs(PECess1);
            totalServiceTax = totalServiceTax + Math.Abs(ServiceTax1);
            totalReceiptTaxable = totalReceiptTaxable + Math.Abs(ReceiptTaxable);
            totalReceiptServiceTax = totalReceiptServiceTax + Math.Abs(ReceiptServiceTax);
            totalReceiptPECess = totalReceiptPECess + Math.Abs(ReceiptPECess);
            totalReceiptHECess = totalReceiptHECess + Math.Abs(ReceiptHECess);

            if (Math.Abs(TaxableAmount) > 0)
            {
                e.Row.Cells[1].Text = acntstng.GetCurrency(Math.Abs(TaxableAmount));
            }
            else
            {
                e.Row.Cells[1].Text = string.Empty;
            }
            if (Math.Abs(NonTaxableAmount) > 0)
            {
                e.Row.Cells[2].Text = acntstng.GetCurrency(Math.Abs(NonTaxableAmount));
            }
            else
            {
                e.Row.Cells[2].Text = string.Empty;
            }
            if (Math.Abs(ReimbursementAmount) > 0)
            {
                e.Row.Cells[3].Text = acntstng.GetCurrency(Math.Abs(ReimbursementAmount));
            }
            else
            {
                e.Row.Cells[3].Text = string.Empty;
            }

            if (Math.Abs(ServiceTax1) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(ServiceTax1));
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }
            if (Math.Abs(PECess1) > 0)
            {
                e.Row.Cells[5].Text = acntstng.GetCurrency(Math.Abs(PECess1));
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;
            }
            if (Math.Abs(HECess1) > 0)
            {
                e.Row.Cells[6].Text = acntstng.GetCurrency(Math.Abs(HECess1));
            }
            else
            {
                e.Row.Cells[6].Text = string.Empty;
            }
            if (Math.Abs(ReceiptTaxable) > 0)
            {
                e.Row.Cells[7].Text = acntstng.GetCurrency(Math.Abs(ReceiptTaxable));
            }
            else
            {
                e.Row.Cells[7].Text = string.Empty;
            }
            if (Math.Abs(ReceiptServiceTax) > 0)
            {
                e.Row.Cells[8].Text = acntstng.GetCurrency(Math.Abs(ReceiptServiceTax));
            }
            else
            {
                e.Row.Cells[8].Text = string.Empty;
            }
            if (Math.Abs(ReceiptPECess) > 0)
            {
                e.Row.Cells[9].Text = acntstng.GetCurrency(Math.Abs(ReceiptPECess));
            }
            else
            {
                e.Row.Cells[9].Text = string.Empty;
            }
            if (Math.Abs(HECess1) > 0)
            {
                e.Row.Cells[10].Text = acntstng.GetCurrency(Math.Abs(ReceiptHECess));
            }
            else
            {
                e.Row.Cells[10].Text = string.Empty;
            }

            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Right;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "Total";
            e.Row.Cells[1].Text = acntstng.GetCurrency(totalTaxable).ToString();
            e.Row.Cells[2].Text = acntstng.GetCurrency(totalNonTaxable).ToString();
            e.Row.Cells[3].Text = acntstng.GetCurrency(totalReimbursementAmount).ToString();
            e.Row.Cells[4].Text = acntstng.GetCurrency(totalServiceTax).ToString();
            e.Row.Cells[5].Text = acntstng.GetCurrency(totalPECess).ToString();
            e.Row.Cells[6].Text = acntstng.GetCurrency(totalHECess).ToString();
            e.Row.Cells[7].Text = acntstng.GetCurrency(totalReceiptTaxable).ToString();
            e.Row.Cells[8].Text = acntstng.GetCurrency(totalReceiptServiceTax).ToString();
            e.Row.Cells[9].Text = acntstng.GetCurrency(totalReceiptPECess).ToString();
            e.Row.Cells[10].Text = acntstng.GetCurrency(totalReceiptHECess).ToString();
          
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.VerticalAlign = VerticalAlign.Bottom;
        }

    }
    protected void btnCangeDate_Click(object sender, EventArgs e)
    {
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }

    protected void grdBillsReceiptView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdBillsReceiptView.PageIndex = e.NewPageIndex;
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }
    protected void grdPrint_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
           e.Row.Cells[0].Attributes.Add("rowspan", "2");
            e.Row.Cells[1].Attributes.Add("colspan", "4");
           // e.Row.Cells.RemoveAt(5);
            e.Row.Cells.RemoveAt(4);
            e.Row.Cells.RemoveAt(3);
            e.Row.Cells.RemoveAt(2);
            e.Row.Cells[1].Text = "Bills";
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[2].Text = "Receipts";
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;
            //new row//

            string strNewRow = "</th></tr><tr><th class=\"gridheader\" style='width:150px'>Taxable</th><th class=\"gridheader\" style='width:150px'>NonTaxable</th><th class=\"gridheader\" style='width:150px'>Total Tax</th><th class=\"gridheader\" style='width:150px'>Total Amt</th><th class=\"gridheader\">Total Amt</th>";
            e.Row.Cells[2].Text += strNewRow;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double TaxableAmountN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TaxableAmount"));
            Double NonTaxableAmountN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "NonTaxableAmount"));
            Double ServiceTax1N = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ServiceTax1"));
            Double PECess1N = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "PECess1"));
            Double HECess1N = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "HECess1"));
            Double ReceiptTaxableN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReceiptTaxable"));
            Double ReceiptServiceTaxN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReceiptServiceTax"));
            Double ReceiptPECessN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReceiptPECess"));
            Double ReceiptHECessN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReceiptHECess"));

            Double TotalAmtBill = TaxableAmountN + NonTaxableAmountN + ServiceTax1N + PECess1N + HECess1N;
            Double TotalAmtRecpt = ReceiptHECessN + ReceiptPECessN + ReceiptServiceTaxN + ReceiptTaxableN;
            Double TotalTaxBill = ServiceTax1N + PECess1N + HECess1N;
            
            totalTaxableN = totalTaxableN + Math.Abs(TaxableAmountN);
            totalNonTaxableN = totalNonTaxableN + Math.Abs(NonTaxableAmountN);
            totalHECessN = totalHECessN + Math.Abs(HECess1N);
            totalPECessN = totalPECessN + Math.Abs(PECess1N);
            totalServiceTaxN = totalServiceTaxN + Math.Abs(ServiceTax1N);
            totalReceiptTaxableN = totalReceiptTaxableN + Math.Abs(ReceiptTaxableN);
            totalReceiptServiceTaxN = totalReceiptServiceTaxN + Math.Abs(ReceiptServiceTaxN);
            totalReceiptPECessN = totalReceiptPECessN + Math.Abs(ReceiptPECessN);
            totalReceiptHECessN = totalReceiptHECessN + Math.Abs(ReceiptHECessN);

            if (Math.Abs(TaxableAmountN) > 0)
            {
                e.Row.Cells[1].Text = acntstng.GetCurrency(Math.Abs(TaxableAmountN));
            }
            else
            {
                e.Row.Cells[1].Text = string.Empty;
            }
            if (Math.Abs(NonTaxableAmountN) > 0)
            {
                e.Row.Cells[2].Text = acntstng.GetCurrency(Math.Abs(NonTaxableAmountN));
            }
            else
            {
                e.Row.Cells[2].Text = string.Empty;
            }
            if (Math.Abs(TotalTaxBill) > 0)
            {
                e.Row.Cells[3].Text = acntstng.GetCurrency(Math.Abs(TotalTaxBill));
            }
            else
            {
                e.Row.Cells[3].Text = string.Empty;
            }

            if (Math.Abs(TotalAmtBill) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(TotalAmtBill));
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }
            if (Math.Abs(TotalAmtRecpt) > 0)
            {
                e.Row.Cells[5].Text = acntstng.GetCurrency(Math.Abs(TotalAmtRecpt));
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;
            }

            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Double TotalBillAmt = totalTaxableN + totalNonTaxableN + totalServiceTaxN + totalPECessN + totalHECessN;
            Double TotalReceiptAmt = totalReceiptTaxableN + totalReceiptServiceTaxN + totalReceiptPECessN + totalReceiptHECessN;
            Double TotalAmtTax = totalServiceTaxN + totalPECessN + totalHECessN;
            e.Row.Cells[0].Text = "Total";
            e.Row.Cells[1].Text = acntstng.GetCurrency(totalTaxableN).ToString();
            e.Row.Cells[2].Text = acntstng.GetCurrency(totalNonTaxableN).ToString();
            e.Row.Cells[3].Text = acntstng.GetCurrency(TotalAmtTax).ToString();
            e.Row.Cells[4].Text = acntstng.GetCurrency(TotalBillAmt).ToString();
            e.Row.Cells[5].Text = acntstng.GetCurrency(TotalReceiptAmt).ToString();

            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.VerticalAlign = VerticalAlign.Bottom;
        }

    }
}
